﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Runtime.InteropServices;
using Trade.DAL;
using Trade.Model;
using Trade.Controls;

namespace Trade
{
    public partial class FrmMain : Form
    {
        public static CurUserEnvironment curUserEnvir = new CurUserEnvironment();
        public static bool login = false;

        public FrmMain()
        {
            InitializeComponent();

            Application.Idle += new EventHandler(Application_Idle);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            SetAllEnabledMenuItem(false);

            FrmLogin loginForm = new FrmLogin();
            loginForm.ShowDialog();

            //登录成功，获取相应的菜单操作权限
            if (login == true)
            {
                User.GetUserPermission(curUserEnvir.userBaseData.UserId, mainMenu);

                this.ssrOperator.Text = "操作员：[ " + curUserEnvir.userBaseData.UserId.Trim() + " ] " + curUserEnvir.userBaseData.UserName.Trim();
                this.tmrDatetime.Start();
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (MessageBox.Show("确定要退出系统吗？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            //{
                Application.Exit();
            
            //}
        }

        //将所有菜单项Disabled
        private void SetAllEnabledMenuItem(bool enabled)
        {
            foreach (ToolStripMenuItem mi in mainMenu.Items)
            {
                mi.Enabled = enabled;
                SetAllEnabledDropDownItems(mi, enabled);
            }
        }

        private void SetAllEnabledDropDownItems(ToolStripMenuItem ctrl, bool enabled)
        {
            if (ctrl.DropDownItems.Count != 0)
            {
                for (int i = 0; i < ctrl.DropDownItems.Count; i++)
                {
                    if (ctrl.DropDownItems[i] is ToolStripMenuItem)
                    {
                        ctrl.DropDownItems[i].Enabled = enabled;
                        SetAllEnabledDropDownItems((ToolStripMenuItem)ctrl.DropDownItems[i], enabled);
                    }
                }
            }
        }

        private void Application_Idle(object sender, EventArgs e)
        {
            if (Control.IsKeyLocked(Keys.NumLock))
                this.ssrNumLock.Text = "Num";
            else
                this.ssrNumLock.Text = "   ";

            if (Control.IsKeyLocked(Keys.CapsLock))
                this.ssrCapsLock.Text = "Caps";
            else
                this.ssrCapsLock.Text = "    ";
        } 

        //相同的ChildForm只显示一次
        private T ShowFormOnlyOnce<T> () where T: Form, new()
        {
            foreach (Form tempForm in this.MdiChildren) //查找当前父表单所有子表单
            {
                if (tempForm.GetType().Equals(typeof(T)))
                {
                    tempForm.Activate();
                    return tempForm as T;
                }
            }

            T newForm = new T();
            newForm.MdiParent = this;
            newForm.Show();
            return newForm;
        }

        private static void ShowFormOnlyOnce(Form mainForm, Type formType)
        {
        }

        //进货单
        private void PurchaseForm_Click(object sender, EventArgs e)
        {
            /*
            PurchaseForm purchaseForm = null;

            foreach (PurchaseForm tempForm in this.MdiChildren) //查找当前父表单所有子表单
            {
                if (tempForm is PurchaseForm)
                {
                    purchaseForm = (PurchaseForm)tempForm;
                    break;
                }
            }

            if (purchaseForm == null || purchaseForm.IsDisposed)
            {
                purchaseForm = new PurchaseForm();
                purchaseForm.MdiParent = this;
            }
            
            purchaseForm.Show();
            purchaseForm.WindowState = FormWindowState.Maximized;
            purchaseForm.Activate();
            */

            FrmPurchaseMain purchaseMainForm = ShowFormOnlyOnce<FrmPurchaseMain>();
            //purchaseMainForm.WindowState = FormWindowState.Maximized;
        }

        private void SalesForm_Click(object sender, EventArgs e)
        {
            FrmSalesMain salesMainForm = ShowFormOnlyOnce<FrmSalesMain>();
            //salesMainForm.WindowState = FormWindowState.Maximized;
        }

        private void WinCascade_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void WinVertical_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void WinHorizontal_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void tmrDatetime_Tick(object sender, EventArgs e)
        {
            this.ssrDatetime.Text = "当前日期时间：" + DateTime.Now.ToString();
            //this.ssrCapsLock.Text = Console.CapsLock == true ? "Caps" : "    ";
            //this.ssrNumLock.Text = Console.NumberLock == true ? "Num" : "   ";
        }

        private void Customer_Click(object sender, EventArgs e)
        {
            FrmCustomer customerForm = ShowFormOnlyOnce<FrmCustomer>();
        }

        private void Vendor_Click(object sender, EventArgs e)
        {
            FrmVendor vendorForm = ShowFormOnlyOnce<FrmVendor>();
        }

        private void ProductData_Click(object sender, EventArgs e)
        {
            FrmProduct productForm = ShowFormOnlyOnce<FrmProduct>();
        }

        private void Employee_Click(object sender, EventArgs e)
        {
            FrmEmployee employeeForm = ShowFormOnlyOnce<FrmEmployee>();
        }

        private void Income_Click(object sender, EventArgs e)
        {
            FrmOtherIncome otherIncomeForm = ShowFormOnlyOnce<FrmOtherIncome>();
        }

        private void Payment_Click(object sender, EventArgs e)
        {
            FrmDailyExpenses dailyExpensesForm = ShowFormOnlyOnce<FrmDailyExpenses>();
        }

        private void IncomeType_Click(object sender, EventArgs e)
        {

        }

        private void BaseClass_Click(object sender, EventArgs e)
        {
            FrmSubjectMessage subjectMessageForm = ShowFormOnlyOnce<FrmSubjectMessage>();
        }

        private void InvoiceManagement_Click(object sender, EventArgs e)
        {
            FrmInvoiceMain invoiceMainForm = ShowFormOnlyOnce<FrmInvoiceMain>();
        }

        private void Underflow_Click(object sender, EventArgs e)
        {
            FrmAdjustMain adjustMainForm = ShowFormOnlyOnce<FrmAdjustMain>();
        } 

        private void Overflow_Click(object sender, EventArgs e)
        {
            FrmAdjustMain adjustMainForm = ShowFormOnlyOnce<FrmAdjustMain>();
        }
    }
}
